package com.ticket.api.controller;

import com.ticket.api.service.TbUserInfoService;
import com.ticket.api.service.TblMarketTicketInfoService;
import com.ticket.common.string.StringUtil;
import com.ticket.common.time.CommonFunction;
import com.ticket.db.dao.TblMarketTicketInfoMapper;
import com.ticket.db.entity.TblMarketTicketInfo;
import com.ticket.db.entity.TblUserInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/ticket")
public class TblMarketTicketInfoController {
    private static Logger logger = LoggerFactory.getLogger(TblMarketTicketInfoController.class);


    @Autowired
    TbUserInfoService userInfoService;
    @Autowired
    TblMarketTicketInfoService tblMarketTicketInfoService;

    @RequestMapping("/getTicketAll")
    public List<TblMarketTicketInfo> findTblMarketTicketInfoAll(String userId) {
        try {
            List<TblMarketTicketInfo> list = tblMarketTicketInfoService.findTblMarketTicketInfoAll(userId);
            for (int i=0;i<list.size();i++) {
                list.get(i).setLastUpdateTime(CommonFunction.Toyymmdd(list.get(i).getLastUpdateTime()));
            }
            return  list;
        }catch (Exception e){
            logger.error("TblMarketTicketInfoController>>findTblMarketTicketInfoAll："+e);
            return null;
        }
    }

    @RequestMapping("/getTicketInfoBy")
    public List<TblMarketTicketInfo> findTblMarketTicketInfoByCondition(TblMarketTicketInfo info) {
        try {
            List<TblMarketTicketInfo> list = tblMarketTicketInfoService.findTblMarketTicketInfoByCondition(info);
            for (int i=0;i<list.size();i++) {
                list.get(i).setLastUpdateTime(CommonFunction.Toyymmdd(list.get(i).getLastUpdateTime()));
                TblUserInfo infoUser = userInfoService.findTblUserInfoById(list.get(i).getCreateUser());
                list.get(i).setAvatar(infoUser.getRemarks());
                list.get(i).setUserName(infoUser.getUserName());
            }
            return list;
        }catch (Exception e){
            logger.error("TblMarketTicketInfoController>>findTblMarketTicketInfoByCondition："+e);
            return null;
        }
    }

    @RequestMapping("/ticketInfoInsert")
    public Integer insertTblMarketTicketInfo(TblMarketTicketInfo info) {

        try {
            return tblMarketTicketInfoService.insertTblMarketTicketInfo(info);
        }catch (Exception e){
            logger.error("TblMarketTicketInfoController>>insertTblMarketTicketInfo："+e);
            return 0;
        }
    }

    @RequestMapping("/ticketInfoUpdate")
    public Integer updateTblMarketTicketInfo(TblMarketTicketInfo info) {
        try {
            return tblMarketTicketInfoService.updateTblMarketTicketInfo(info);
        }catch (Exception e){
            logger.error("TblMarketTicketInfoController>>updateTblMarketTicketInfo："+e);
            return 0;
        }

    }
}
